package xzx.mcp.tool;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;

/**
 * @className: StatsTool
 * @author: xzx
 * @date: 2025-10-22
 **/
@Component
@Slf4j
public class StatsTool {

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class VehicleStatsInfo {
        @ToolParam(description = "截止日期(可以为空)")
        private String period;
        @ToolParam(description = "统计的农机类型(可以为空)")
        private String vehicleType;
        @ToolParam(description = "统计的地区名称(可以为空)")
        private String regionName;
    }

    @Tool(name = "getAnnualStats",description = "综合统计农机车辆信息")
    public String getAnnualStats(VehicleStatsInfo vehicleStatsInfo) {
        log.info("========== 调用MCP工具：getAnnualStats() ==========");
        log.info(String.format("========== 参数 period：%s ==========", vehicleStatsInfo.getPeriod()));
        log.info(String.format("========== 参数 vehicleType：%s ==========", vehicleStatsInfo.getVehicleType()));
        if (StringUtils.isEmpty(vehicleStatsInfo.getPeriod())){
            vehicleStatsInfo.setPeriod("2025-10-22");
        }
        return String.format(
                "截至%s，全区农机总量达%d台，其中新注册%d台（同比增长%.2f%%）。大型拖拉机占比%d%%，较上年提升%d个百分点。",
                vehicleStatsInfo.getPeriod(), 100, 200, 15.01, 30,30
        );
    }

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RegisterVehicleInfoParam {
        @ToolParam(description = "年份")
        private Integer year;
    }
    @Tool(name = "getRegisterVehicleInfo",description = "统计每年农机注册的数量")
    public String getRegisterVehicleInfo(RegisterVehicleInfoParam vehicleInfoParam) {
        log.info("========== 调用MCP工具：getRegisterVehicleInfo() ==========");
        log.info(String.format("========== 参数 year：%s ==========", vehicleInfoParam.getYear()));

        return String.format(
                "截至%d年,农机车辆注册数量:%d台,其中G1轮式拖拉机:%d台,G2轮式拖拉机运输机组:%d台,K1手扶拖拉机:%d台,K2手扶拖拉机运输机组:%d台,R轮式联合收割机:%d台,S履带式联合收割机:%d台,L履带拖拉机:%d台",
                vehicleInfoParam.getYear(), 100, 200, 3, 30,30,3,3,6
        );
    }
}
